﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using System.IO;
using Ext.Net;
using Ext.Net.MVC;
using WebEntities;
using WebEntities.Models.ProductInfo;
using com.subb.ProductInfo;

namespace WebUI.Areas.WebAdmin.Controllers
{
    [Authorize]
    public class RecomProductController : Controller
    {
        private const string ProductIndexCoverPath = "ProductIndexCover/";

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetAllRecomProduct(StoreRequestParameters parms)
        {
            PP_RecomProductDAL rdal = new PP_RecomProductDAL();
            int ttlCount;
            List<PP_RecommendProduct> wiRprodList = rdal.GetAllRecomProduct(parms.Start, parms.Limit, out ttlCount);
            List<RecomProduct> rprodList = new List<RecomProduct>();
            foreach (var wiprod in wiRprodList)
            {
                RecomProduct rprod = RecomProduct.AsRecomProduct(wiprod);
                rprodList.Add(rprod);
            }
            return this.Store(rprodList, ttlCount);
        }

        [HttpPost]
        public ActionResult DeleteRecomProduct(List<RecomProduct> delProds)
        {
            try
            {
                PP_RecomProductDAL rdal = new PP_RecomProductDAL();
                foreach (var prod in delProds)
                {
                    rdal.RemoveRecomProduct(prod.RecomID);
                }
                return this.Direct(new { Success = true, Msg = "删除成功！" });
            }
            catch (Exception ex)
            {
                return this.Direct(new { Success = false, Msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message });
            }
        }

        public ActionResult NewRecomProductWindow()
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "新建推荐产品",
                Width = 500,
                Height = 300,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.TextListBullets,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("CreateRecomProduct"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Render();
            return this.Direct();
        }

        public ActionResult CreateRecomProduct()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateRecomProduct(RecomProduct rprod)
        {
            FormPanelResult response = new FormPanelResult();
            response.IsUpload = true;
            try
            {
                PP_RecomProductDAL rdal = new PP_RecomProductDAL();
                int recomId = rdal.CreateRecomProduct(rprod);
                //上传文件
                if (Request.Files.Count > 0)
                {
                    HttpFileCollectionBase fb = Request.Files;
                    HttpPostedFileBase fpfb1 = fb.Get("IndexCover");
                    string savePath = string.Empty;
                    //上传产品首页封面图
                    if (fpfb1 != null && fpfb1.ContentLength > 0)
                    {
                        string uploadVFolder = ConfigurationManager.AppSettings["UploadVFolder"];
                        if (!string.IsNullOrWhiteSpace(uploadVFolder))
                        {
                            savePath = Server.MapPath(uploadVFolder + ProductIndexCoverPath);
                            if (!Directory.Exists(savePath))
                            {
                                Directory.CreateDirectory(savePath);
                            }
                            string extName = Path.GetExtension(fpfb1.FileName);
                            savePath += recomId + extName;
                            fpfb1.SaveAs(savePath);
                            //更新路径
                            string webpath = uploadVFolder + ProductIndexCoverPath + recomId + extName;
                            RecomProduct updProd = RecomProduct.AsRecomProduct(rdal.GetRecomProduct(recomId));
                            updProd.IndexCover = webpath;
                            rdal.UpdateRecomProduct(recomId, updProd);
                        }
                    }
                }
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "推荐产品创建成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }
    }
}
